package stats.calculator;

/**
 * Title: StatsCalculator
 * Description:
 *
 * @author Yufan
 * @version 1.0.0
 * @since 2016-05-09 20:19
 */
public class StatsCalculator {

    private final Command minCommand = new MinCommand();

    private final Command maxCommand = new MaxCommand();

    private final Command sumCommand = new SumCommand();

    public int minOf(int... values) {
        return execute(minCommand, values);
    }

    public int maxOf(int... values) {
        return execute(maxCommand, values);
    }

    private int execute(final Command command, int... values) {
        if (values.length == 0) {
            throw new IllegalArgumentException("input elements should not be null");
        }
        int returnValue = values[0];
        for (int i = 1; i < values.length; i++) {
            returnValue = command.visit(returnValue, values[i]);
        }
        return returnValue;
    }

    public double average(int... values) {
        int sum = execute(sumCommand, values);
        return ((double) sum) / values.length;
    }

    public int getLength(int... values) {
        return values == null ? 0 : values.length;
    }


    private static class MinCommand implements Command {
        public int visit(int origin, int value) {
            return Math.min(origin, value);
        }
    }

    private static class MaxCommand implements Command {
        public int visit(int origin, int value) {
            return Math.max(origin, value);
        }
    }

    private static class SumCommand implements Command {
        public int visit(int origin, int value) {
            return origin + value;
        }
    }
}
